package com.wyy.service.impl;

import com.wyy.mapper.OrderMapper;
import com.wyy.pojo.AppreciatesOrder;
import com.wyy.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-05-23 15:36
 */
@Service
public class ScheduledService {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 定时删除超时的订单，每隔30分钟执行一次
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    @Async
    public void deleteTimeOutOrder() {
        List<AppreciatesOrder> AppreciatesOrders = orderMapper.getAllOrder();
        AppreciatesOrders.forEach(order -> {
            String orderId = order.getOrderId();
            String orderDate = order.getOrderDate();
            boolean flag = false;
            long nowLongDate = System.currentTimeMillis();
            long orderLongTime = MyUtil.parseTimeToString(orderDate);
            if (order.getOrderStatus() == 0 && (nowLongDate - orderLongTime) > (1000 * 60 * 30)) {
                flag = true;
            }
            if (flag) {
                orderMapper.deleteOrderByOrderId(orderId);
            }
        });
    }
}